{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0a8550bf-735d-4866-95bd-3532030d1ed4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# df = pd.read_csv('603444SH.csv')\n",
    "# print(df.head(25))\n",
    "# df.iloc[0:16, :].T\n",
    "# for i in range(0, len(df), 18):\n",
    "    # print(df.iloc[i:i + 18, :].T.iloc[1:2, :].to_csv('demo.csv', mode='a', index=False, header=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5bfe77f-d2a1-48a5-99d5-2784cd788698",
   "metadata": {},
   "source": [
    "# 处理数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4a5c7c07-b1e6-4c1c-a950-cf76853183e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     Unnamed: 0       amount                           askPrice  \\\n",
      "7125  603444.SH  236525100.0  [168.78, 168.79, 169.0, 0.0, 0.0]   \n",
      "7126        NaN       amount                           askPrice   \n",
      "7127  603444.SH  236525100.0  [168.78, 168.79, 169.0, 0.0, 0.0]   \n",
      "7128        NaN       amount                           askPrice   \n",
      "7129  603444.SH  236541900.0  [168.78, 168.79, 169.0, 0.0, 0.0]   \n",
      "\n",
      "               askVol                            bidPrice           bidVol  \\\n",
      "7125  [6, 5, 1, 0, 0]  [168.37, 168.36, 168.35, 0.0, 0.0]  [1, 3, 5, 0, 0]   \n",
      "7126           askVol                            bidPrice           bidVol   \n",
      "7127  [6, 5, 1, 0, 0]  [168.37, 168.36, 168.35, 0.0, 0.0]  [1, 3, 5, 0, 0]   \n",
      "7128           askVol                            bidPrice           bidVol   \n",
      "7129  [6, 5, 1, 0, 0]  [168.36, 168.35, 168.34, 0.0, 0.0]  [3, 5, 1, 0, 0]   \n",
      "\n",
      "        high  lastClose  lastPrice  lastSettlementPrice     low    open  \\\n",
      "7125  178.16     180.02     168.37                  0.0  164.01  177.02   \n",
      "7126    high  lastClose  lastPrice  lastSettlementPrice     low    open   \n",
      "7127  178.16     180.02     168.37                  0.0  164.01  177.02   \n",
      "7128    high  lastClose  lastPrice  lastSettlementPrice     low    open   \n",
      "7129  178.16     180.02     168.37                  0.0  164.01  177.02   \n",
      "\n",
      "      openInt  pvolume  settlementPrice  stockStatus            timetag  \\\n",
      "7125       13  1393876              0.0            0  20240205 11:29:53   \n",
      "7126  openInt  pvolume  settlementPrice  stockStatus            timetag   \n",
      "7127       13  1393876              0.0            0  20240205 11:29:53   \n",
      "7128  openInt  pvolume  settlementPrice  stockStatus            timetag   \n",
      "7129       13  1393976              0.0            0  20240205 11:29:59   \n",
      "\n",
      "      volume  \n",
      "7125   13939  \n",
      "7126  volume  \n",
      "7127   13939  \n",
      "7128  volume  \n",
      "7129   13940  \n"
     ]
    }
   ],
   "source": [
    "prepare_process_data = pd.read_csv('603444SHTest.csv')\n",
    "print(prepare_process_data.tail())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e700de4f-20af-46d1-a705-d7c8e3fb724c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('603444.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "820252a3-39eb-4b18-939f-e483a4fa175b",
   "metadata": {},
   "source": [
    "# 海龟"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "338c5338-8ace-4c75-8d2f-0c0db2cb38e2",
   "metadata": {},
   "source": [
    "## https://www.myquant.cn/docs/python_strategyies/110"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8a7e855c-bf74-4309-a260-1fab4b957c7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9201782a-b4d4-4d69-b563-eae5b693b405",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         date    open    high     low   close  volume\n",
      "0  2017-01-04   77.76   77.76   77.76   77.76      88\n",
      "1  2017-01-05   85.54   85.54   85.54   85.54      44\n",
      "2  2017-01-06   94.09   94.09   94.09   94.09      40\n",
      "3  2017-01-09  103.50  103.50  103.50  103.50     103\n",
      "4  2017-01-10  113.85  113.85  113.85  113.85     166\n"
     ]
    }
   ],
   "source": [
    "print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "eae200a1-6b90-4234-a1b8-9ddd5622ca4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       2017-01-04\n",
       "1       2017-01-05\n",
       "2       2017-01-06\n",
       "3       2017-01-09\n",
       "4       2017-01-10\n",
       "           ...    \n",
       "1715    2024-01-23\n",
       "1716    2024-01-24\n",
       "1717    2024-01-25\n",
       "1718    2024-01-26\n",
       "1719    2024-01-29\n",
       "Name: date, Length: 1720, dtype: object"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "79d37675-b3d8-4875-aa37-a13340dc369d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           date    open    high     low   close  volume\n",
      "0    2017-01-04   77.76   77.76   77.76   77.76      88\n",
      "1    2017-01-05   85.54   85.54   85.54   85.54      44\n",
      "2    2017-01-06   94.09   94.09   94.09   94.09      40\n",
      "3    2017-01-09  103.50  103.50  103.50  103.50     103\n",
      "4    2017-01-10  113.85  113.85  113.85  113.85     166\n",
      "...         ...     ...     ...     ...     ...     ...\n",
      "1715 2024-01-23  196.01  207.87  196.01  204.96   24196\n",
      "1716 2024-01-24  206.00  207.00  195.57  201.63   16086\n",
      "1717 2024-01-25  201.81  206.60  200.08  205.60   20580\n",
      "1718 2024-01-26  208.06  211.31  203.00  203.28   21418\n",
      "1719 2024-01-29  203.81  204.50  192.68  194.83   18876\n",
      "\n",
      "[1720 rows x 6 columns]\n"
     ]
    }
   ],
   "source": [
    "df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "a246e84f-c1be-4d69-a4f2-13190597b5af",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_df =  df[df['date'].between('2023-01-01', '2024-02-01')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "ffab9b18-35b0-4009-ad32-fddd81dcb7c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1458</th>\n",
       "      <td>2023-01-03</td>\n",
       "      <td>313.00</td>\n",
       "      <td>324.50</td>\n",
       "      <td>310.62</td>\n",
       "      <td>323.50</td>\n",
       "      <td>5349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1459</th>\n",
       "      <td>2023-01-04</td>\n",
       "      <td>323.60</td>\n",
       "      <td>342.50</td>\n",
       "      <td>316.11</td>\n",
       "      <td>339.09</td>\n",
       "      <td>10911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1460</th>\n",
       "      <td>2023-01-05</td>\n",
       "      <td>339.03</td>\n",
       "      <td>365.00</td>\n",
       "      <td>339.03</td>\n",
       "      <td>351.20</td>\n",
       "      <td>11851</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1461</th>\n",
       "      <td>2023-01-06</td>\n",
       "      <td>350.06</td>\n",
       "      <td>356.00</td>\n",
       "      <td>347.00</td>\n",
       "      <td>354.60</td>\n",
       "      <td>5649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1462</th>\n",
       "      <td>2023-01-09</td>\n",
       "      <td>354.38</td>\n",
       "      <td>363.90</td>\n",
       "      <td>354.38</td>\n",
       "      <td>363.00</td>\n",
       "      <td>8591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1715</th>\n",
       "      <td>2024-01-23</td>\n",
       "      <td>196.01</td>\n",
       "      <td>207.87</td>\n",
       "      <td>196.01</td>\n",
       "      <td>204.96</td>\n",
       "      <td>24196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1716</th>\n",
       "      <td>2024-01-24</td>\n",
       "      <td>206.00</td>\n",
       "      <td>207.00</td>\n",
       "      <td>195.57</td>\n",
       "      <td>201.63</td>\n",
       "      <td>16086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1717</th>\n",
       "      <td>2024-01-25</td>\n",
       "      <td>201.81</td>\n",
       "      <td>206.60</td>\n",
       "      <td>200.08</td>\n",
       "      <td>205.60</td>\n",
       "      <td>20580</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1718</th>\n",
       "      <td>2024-01-26</td>\n",
       "      <td>208.06</td>\n",
       "      <td>211.31</td>\n",
       "      <td>203.00</td>\n",
       "      <td>203.28</td>\n",
       "      <td>21418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1719</th>\n",
       "      <td>2024-01-29</td>\n",
       "      <td>203.81</td>\n",
       "      <td>204.50</td>\n",
       "      <td>192.68</td>\n",
       "      <td>194.83</td>\n",
       "      <td>18876</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>262 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           date    open    high     low   close  volume\n",
       "1458 2023-01-03  313.00  324.50  310.62  323.50    5349\n",
       "1459 2023-01-04  323.60  342.50  316.11  339.09   10911\n",
       "1460 2023-01-05  339.03  365.00  339.03  351.20   11851\n",
       "1461 2023-01-06  350.06  356.00  347.00  354.60    5649\n",
       "1462 2023-01-09  354.38  363.90  354.38  363.00    8591\n",
       "...         ...     ...     ...     ...     ...     ...\n",
       "1715 2024-01-23  196.01  207.87  196.01  204.96   24196\n",
       "1716 2024-01-24  206.00  207.00  195.57  201.63   16086\n",
       "1717 2024-01-25  201.81  206.60  200.08  205.60   20580\n",
       "1718 2024-01-26  208.06  211.31  203.00  203.28   21418\n",
       "1719 2024-01-29  203.81  204.50  192.68  194.83   18876\n",
       "\n",
       "[262 rows x 6 columns]"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "04dcc07e-5c00-4f15-9817-8c0b27e858ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "high_price_trade_date = 5\n",
    "low_price_trade_date = 1\n",
    "capital = 150000\n",
    "hold = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "83c9d567-52c3-4085-b972-0b9360899d9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "买了4手,余额:2896.0,花费:147104.00\n",
      "卖了4手,余额:146332.0, 金额:143436.0\n",
      "买了3手,余额:26332.0,花费:120000.00\n",
      "卖了3手,余额:170275.0, 金额:143943.0\n",
      "买了3手,余额:20575.0,花费:149700.00\n",
      "卖了3手,余额:182599.0, 金额:162024.0\n"
     ]
    }
   ],
   "source": [
    "for index, row in test_df.iterrows():\n",
    "    max_price = max(df[index-high_price_trade_date:index]['high'])\n",
    "    min_price = min(df[index-low_price_trade_date:index]['low'])\n",
    "    open = row['open']\n",
    "    low = row['low']\n",
    "    last_price = open * 100 \n",
    "    if open > max_price and capital >= last_price:\n",
    "        can_buy = int(capital / (open*100))\n",
    "        capital = capital - can_buy*(open*100)\n",
    "        hold= hold+can_buy\n",
    "        print(f'买了{can_buy}手,余额:{capital},花费:{can_buy*(open*100):.2f}')\n",
    "    elif open < min_price and hold > 0:\n",
    "        sell_money = hold * 100 * low\n",
    "        capital = capital + sell_money\n",
    "        print(f'卖了{hold}手,余额:{capital}, 金额:{sell_money}')\n",
    "        hold = 0\n",
    "    # print(index, row['date_column'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "0f1ab5d7-bc29-4cf5-835e-fa35bcc90896",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "182599.0"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "capital"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43570c46-5aad-4353-b66b-03c18461ea9f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
